package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qut.graduate.busrtquery.BRTQCache;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.bean.BusStation;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;
import com.qut.graduate.busrtquery.utils.common.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class BusRealTimeStationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "BusRealTimeStationAdapter";
    private Context mContext;
    private List<BusStation> mBusStationList;
    private List<BusStation> mBusStationListCopy;
    private int mDirect = BRTQConstant.BUS_DIRECT_UP; // 公交线路方向，1为上行，0为下行

    public BusRealTimeStationAdapter(Context context, List<BusStation> mBusStationList) {
        this.mContext = context;
        this.mBusStationList = mBusStationList;
        LogUtils.d("----实时公交信息界面  BusRealTimeStationAdapter mBusStationList: " + mBusStationList, getClass());
        mBusStationListCopy = new ArrayList<>();
        for (int i = 0; i < mBusStationList.size(); i++) {
            if (mBusStationList.get(i).getDirection().equals(String.valueOf(BRTQConstant.BUS_DIRECT_UP))) {
                mBusStationListCopy.add(mBusStationList.get(i));
            }
        }
        BRTQCache.getInstance().setUpRouteSize(mBusStationListCopy.size());
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return BRTQConstant.BUS_REAL_TIME_STATION_FIRST; // 起点汽车站
        if (position == getItemCount() - 1) return BRTQConstant.BUS_REAL_TIME_STATION_LAST; // 终点汽车站
        if (position > 0 && position < getItemCount() - 1)
            return BRTQConstant.BUS_REAL_TIME_STATION_MIDDLE; // 中间汽车站
        return BRTQConstant.BUS_REAL_TIME_STATION_MIDDLE; // 其他情况
    }

    @Override
    public int getItemCount() {
        return mBusStationListCopy != null ? mBusStationListCopy.size() : 0;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == BRTQConstant.BUS_REAL_TIME_STATION_FIRST) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bus_real_time_station_start, parent, false);
            return new FirstHolder(view);
        }
        if (viewType == BRTQConstant.BUS_REAL_TIME_STATION_LAST) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bus_real_time_station_end, parent, false);
            return new LastHolder(view);
        }
        if (viewType == BRTQConstant.BUS_REAL_TIME_STATION_MIDDLE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bus_real_time_station, parent, false);
            return new MiddleHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        if (holder instanceof FirstHolder) {
            FirstHolder firstHolder = (FirstHolder) holder;
            firstHolder.mStationName.setText(mBusStationListCopy.get(position).getStaionname());
        }
        if (holder instanceof LastHolder) {
            LastHolder lastHolder = (LastHolder) holder;
            lastHolder.mStationName.setText(mBusStationListCopy.get(position).getStaionname());
        }
        if (holder instanceof MiddleHolder) {
            MiddleHolder middleHolder = (MiddleHolder) holder;
            middleHolder.mStationName.setText(mBusStationListCopy.get(position).getStaionname());
        }
    }

    public void setmDirect(int mDirect){
        this.mDirect = mDirect;
        mBusStationListCopy.clear();
        for (int i = 0; i < mBusStationList.size(); i++) {
            if (mDirect == BRTQConstant.BUS_DIRECT_UP && mBusStationList.get(i).getDirection().equals(String.valueOf(BRTQConstant.BUS_DIRECT_UP))) {
                mBusStationListCopy.add(mBusStationList.get(i));
            } else if (mDirect == BRTQConstant.BUS_DIRECT_DOWN && mBusStationList.get(i).getDirection().equals(String.valueOf(BRTQConstant.BUS_DIRECT_DOWN))) {
                mBusStationListCopy.add(mBusStationList.get(i));
            }
        }
        notifyDataSetChanged();
    }

    public class FirstHolder extends RecyclerView.ViewHolder {
        public TextView mStationName;

        public FirstHolder(View view) {
            super(view);
            mStationName = view.findViewById(R.id.station_name);
        }
    }

    public class LastHolder extends RecyclerView.ViewHolder {
        public TextView mStationName;

        public LastHolder(View view) {
            super(view);
            mStationName = view.findViewById(R.id.station_name);
        }
    }

    public class MiddleHolder extends RecyclerView.ViewHolder {
        public TextView mStationName;

        public MiddleHolder(View view) {
            super(view);
            mStationName = view.findViewById(R.id.station_name);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

}